﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Newtonsoft.Json.Linq;

namespace Controller.Helper
{
    public class Product
    {
        public static string GetProductToSale()
        {
            using (var nawaDataContext = new DCSalePriceDataContext())
            {
                int lastestRound = nawaDataContext.tblNDPrices.Max(p => p.NDRound);
                //int lastestRound = nawaDataContext.tblNDPrices.Where(p => p.ValidON < Document.GetDocumentByID(docID).Created).Max(n => n.NDRound);
                var allProduct = from prodcate in nawaDataContext.viewProductCates
                                 join ndprice in nawaDataContext.tblNDPriceDetails on prodcate.mprodh_grpID equals ndprice.mprodh_grpID
                                 where ndprice.NDRound == lastestRound
                                 orderby prodcate.mprodh_sort 
                                  select new
                                  {
                                      mprodh_grpID = Convert.ToInt32(prodcate.mprodh_grpID),
                                      mprodh_grpname = prodcate.mprodh_grpname,
                                      ndprice.discountPrice,
                                      ndprice.NDApproverLevel1,
                                      ndprice.NDApproverLevel2,
                                      ndprice.NDApproverLevel3
                                  };
                
                JObject k = JObject.FromObject(new
                {
                    AllProduct = allProduct
                });
                return k.ToString();
            }
        }
        public static string GetProductToSale(string docID)
        {
            using (var nawaDataContext = new DCSalePriceDataContext())
            {
                //int lastestRound = nawaDataContext.tblNDPrices.Max(p => p.NDRound);
                int lastestRound = nawaDataContext.tblNDPrices.Where(p => p.ValidON < Document.GetDocumentByID(docID).Created).Max(n => n.NDRound);
                var allProduct = from prodcate in nawaDataContext.viewProductCates
                                 join ndprice in nawaDataContext.tblNDPriceDetails on prodcate.mprodh_grpID equals ndprice.mprodh_grpID
                                 where ndprice.NDRound == lastestRound
                                 orderby prodcate.mprodh_sort
                                 select new
                                 {
                                     mprodh_grpID = Convert.ToInt32(prodcate.mprodh_grpID),
                                     prodcate.mprodh_grpname,
                                     ndprice.discountPrice,
                                     ndprice.NDApproverLevel1,
                                     ndprice.NDApproverLevel2,
                                     ndprice.NDApproverLevel3
                                 };

                JObject k = JObject.FromObject(new
                {
                    AllProduct = allProduct
                });
                return k.ToString();
            }
        }
    }
}
